//
//  NewsViewController.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/29.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsCollectionView.h"
#import "NewsModel.h"
#import "TopScrollView.h"
#import "News24HoursViewController.h"

@interface NewsViewController ()

@end

@implementation NewsViewController {
    NewsCollectionView* _collectionView;
    
    TopScrollView* _topScrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.userInteractionEnabled = YES;
    [self _setNavBar];
    
    [self _createSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - navBar
- (void)_setNavBar {
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar_netease"]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"top_navi_bell_normal@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(news24HoursAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"top_navigation_square@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(weatherAction)];
}

- (void)news24HoursAction {
    News24HoursViewController* news24VC = [[News24HoursViewController alloc] init];
    [self.navigationController pushViewController:news24VC animated:YES];
}

- (void)weatherAction {
    
}

#pragma mark - 子视图
- (void)_createSubViews {
    //collectionView
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(kWidth, kHeight-30-tabBarHeight-10);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//滑动方向水平
    _collectionView = [[NewsCollectionView alloc] initWithFrame:CGRectMake(0, 30-15, kWidth, kHeight-30-tabBarHeight) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    
    
    //位于顶部的滑动视图，各个按钮导航
    _topScrollView = [[TopScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    _topScrollView.tag = 10086;
    _topScrollView.contentSize = CGSizeMake(kWidth/5*7, 30);
    _topScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_topScrollView];
    
    //接收通知,当topScrollView的index变化后，collectionView切换cell
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(topScrollViewIndexChangedAction:) name:@"TopScrollViewIndexChangedNotification" object:nil];
}

- (void)topScrollViewIndexChangedAction:(NSNotification*)notification {
    NSDictionary* userInfo = notification.userInfo;
    
    NSInteger index = [userInfo[@"index"] integerValue];
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"TopScrollViewIndexChangedNotification" object:nil];
}



@end
